package com.edu4java.minitennis5;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Ball {
	int x = 0;
	int y = 0;
	int xa = 1;
	int ya = 1;
	private Game game;

	public Ball(Game game) {
		this.game= game;
	}

	void move() {
		if (x + xa < 0)
			xa = 5;
		if (x + xa > game.getWidth() - 30)
			xa = -5;
		if (y + ya < 0)
			ya = 5;
		if (y + ya > game.getHeight() - 30)
			ya = -5;
		if (collision()){
			ya = -5;
			y = game.racquet.getTopY() - 30;
			//x = game.racquet1.getTopy() - 30;
		}
		if (collision1()){
			ya = -5;
			//y = game.racquet.getTopX() - 30;
			x = game.racquet1.getTopX() - 10;
		}

		x = x + xa;
		y = y + ya;
	}
	
	private boolean collision1() {
		return game.racquet1.getBounds().intersects(getBounds());
		}

	private boolean collision() {
		return game.racquet.getBounds().intersects(getBounds());
		}
	public Rectangle getBounds() {
		return new Rectangle(x, y, 30, 30);
	}
	public void paint(Graphics2D j) {
		j.fillOval(x, y, 30, 30);
		j.setColor(Color.RED);
		
	}
}